package com.cheyun.netsalev3.view

import androidx.lifecycle.Observer
import androidx.lifecycle.ViewModel
import androidx.lifecycle.ViewModelProviders
import androidx.recyclerview.widget.RecyclerView
import com.cheyun.netsalev3.R
import com.cheyun.netsalev3.adapter.MyPagedListAdapter
import com.cheyun.netsalev3.bean.Row
import com.cheyun.netsalev3.databinding.ActivityCuesAllocationBinding
import com.cheyun.netsalev3.utils.BaseActivity
import com.cheyun.netsalev3.utils.InjectorUtils
import com.cheyun.netsalev3.utils.api.NetworkState
import com.cheyun.netsalev3.viewmodel.CuesAllocationActivityViewModel

class CuesAllocationActivity : BaseActivity<ActivityCuesAllocationBinding>() {


    lateinit var adapter: MyPagedListAdapter<Row>
    override fun initContentView(): Int {
        return R.layout.activity_cues_allocation
    }

    override fun initViewModel(): ViewModel? {
        var factory = InjectorUtils.getCuesAllocationActivityViewModelFactory()
        return ViewModelProviders.of(this, factory).get(CuesAllocationActivityViewModel::class.java)
    }


    override fun initView() {
        adapter = MyPagedListAdapter<Row>(R.layout.cues_allocation_item, binding.viewModel)
        var recyclerView = binding.root.findViewById<RecyclerView>(R.id.swipe_target)
        recyclerView.adapter = adapter
        binding.viewModel?.listData?.observe(this, Observer {
            it?.apply {
                adapter.submitList(it)
            }
        })

        binding.swipeRefreshLayout.setOnRefreshListener {
            binding.viewModel?.refresh()
        }

    }


    override fun initData() {

        binding.viewModel?.networkState?.observe(this, Observer {
            binding.swipeRefreshLayout.isRefreshing = it == NetworkState.LOADING
            adapter.setNetworkState(it)
        })
    }

}
